package cn.iocoder.yudao.module.erp.service.business.order.orderquotation;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderquotation.vo.OrderQuotationPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderquotation.vo.OrderQuotationSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.order.orderquotation.OrderQuotationDO;
import cn.iocoder.yudao.module.erp.dal.mysql.business.order.orderquotation.OrderQuotationMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.ORDER_QUOTATION_NOT_EXISTS;

/**
 * 报价方式 Service 实现类
 *
 * @author yy
 */
@Service
@Validated
public class OrderQuotationServiceImpl implements OrderQuotationService {

    @Resource
    private OrderQuotationMapper orderQuotationMapper;

    @Override
    public Long createOrderQuotation(OrderQuotationSaveReqVO createReqVO) {
        // 插入
        OrderQuotationDO orderQuotation = BeanUtils.toBean(createReqVO, OrderQuotationDO.class);
        orderQuotationMapper.insert(orderQuotation);
        // 返回
        return orderQuotation.getId();
    }

    @Override
    public void updateOrderQuotation(OrderQuotationSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderQuotationExists(updateReqVO.getId());
        // 更新
        OrderQuotationDO updateObj = BeanUtils.toBean(updateReqVO, OrderQuotationDO.class);
        orderQuotationMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderQuotation(Long id) {
        // 校验存在
        validateOrderQuotationExists(id);
        // 删除
        orderQuotationMapper.deleteById(id);
    }

    private void validateOrderQuotationExists(Long id) {
        if (orderQuotationMapper.selectById(id) == null) {
            throw exception(ORDER_QUOTATION_NOT_EXISTS);
        }
    }

    @Override
    public OrderQuotationDO getOrderQuotation(Long id) {
        return orderQuotationMapper.selectById(id);
    }

    @Override
    public PageResult<OrderQuotationDO> getOrderQuotationPage(OrderQuotationPageReqVO pageReqVO) {
        return orderQuotationMapper.selectPage(pageReqVO);
    }

}